package cn.ztydata.teachingsystem.heaven.dao;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.common.WebContext;
import cn.ztydata.teachingsystem.heaven.cons.Roles;
import cn.ztydata.teachingsystem.heaven.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 发言回复
 *
 * @author lyj
 * @since 2015-3-20
 */
@Repository
public class CommentDao extends BaseDao {
    @Autowired
    private WebContext webContext;


    /**
     * 新增回复
     *
     * @param comment 回复实体
     * @return int
     * @author lyj
     * @since 2015-3-23
     */
    public int addComment(Comment comment){

        StringBuilder sql = new StringBuilder("INSERT INTO t_comment VALUES (NULL, :sContent, :iCreatorId, :tiRole, NOW(), :iSpeakId, :iCommentId, :sComCreatorName)");

        MapSqlParameterSource param = new MapSqlParameterSource();
        param.addValue("sContent", comment.getContent());

        param.addValue("iCreatorId", webContext.getUserId());
        param.addValue("tiRole", webContext.getRole() == Roles.STUDENT ? 1 : 0);
        param.addValue("iSpeakId", comment.getSpeakId());
        param.addValue("iCommentId", comment.getCommentId());
        param.addValue("sComCreatorName", comment.getComCreatorName());

        log.debug("新增回复");
        return namedParameterJdbcTemplate.update(sql.toString(), param);
    }


    /**
     * 删除回复
     *
     * @param id 回复ID

     * @return int
     * @author lyj
     * @since 2015-3-23
     */
    public int deleteComment(final long id){
        StringBuilder sql = new StringBuilder("DELETE FROM t_comment WHERE id = :id");

        log.debug("删除回复");
        return namedParameterJdbcTemplate.update(sql.toString(), new MapSqlParameterSource().addValue("id", id));
    }



    /**
     * 根据发言ID删除回复
     *
     * @param speakId 发言ID
     * @return int
     * @author lyj
     * @since 2015-3-23
     */
    public int deleteBySpeakId(final long speakId){
        StringBuilder sql = new StringBuilder("DELETE FROM t_comment WHERE iSpeakId = :iSpeakId");

        log.debug("根据发言ID删除回复");
        return namedParameterJdbcTemplate.update(sql.toString(), new MapSqlParameterSource().addValue("iSpeakId", speakId));
    }


    /**
     * 获取回复列表
     *
     * @param speakId 发言ID
     * @param pageNum 当前页码
     * @param pageSize 每页条数

     * @return page
     * @author lyj
     * @since 2015-3-23
     */
    public Page<Comment> getList(final long speakId, final int pageNum, final int pageSize){
        StringBuilder sql = new StringBuilder("SELECT * FROM t_comment");
        StringBuilder sqlCount = new StringBuilder("SELECT COUNT(*) FROM t_comment");

        MapSqlParameterSource param = new MapSqlParameterSource().addValue("iSpeakId", speakId);

        StringBuilder where = new StringBuilder(" WHERE iSpeakId = :iSpeakId");

        //获取总条数
        int total = namedParameterJdbcTemplate.queryForObject(sqlCount.append(where).toString(), param, Integer.class);

        if (total == 0) {
            log.debug("获取总条数为零：total == 0");
            return new Page<>();
        }

        //追加排序
        where.append(" ORDER BY id,dtCreate DESC");

        //追加limit限制
        where.append(getLimitStr(pageNum, pageSize));

        List<Comment> commentList = namedParameterJdbcTemplate.query(sql.append(where).toString(), param, new RowMapper<Comment>() {
            @Override
            public Comment mapRow(ResultSet rs, int rowNum) throws SQLException {
                Comment comment = new Comment();

                comment.setId(rs.getLong("id"));
                comment.setContent(rs.getString("sContent"));
                comment.setCreatorId(rs.getLong("iCreatorId"));
                comment.setRole(rs.getInt("tiRole"));
                comment.setCreate(rs.getTimestamp("dtCreate"));
                comment.setSpeakId(rs.getLong("iSpeakId"));
                comment.setCommentId(rs.getLong("iCommentId"));
                comment.setComCreatorName(rs.getString("sComCreatorName"));

                return comment;
            }
        });

        log.debug("获取回复列表");
        return new Page<>(total, pageSize, pageNum, commentList);
    }


    /**
     * 根据Id获取回复信息
     *
     * @param commentId 回复ID
     * @return Comment
     * @author lyj
     * @since 2015-5-11
     */
    public int getById(final long commentId) {
        StringBuilder sql = new StringBuilder("SELECT COUNT(id) FROM t_comment WHERE id = :id AND iCreatorId = :creatorId AND tiRole = :role");

        MapSqlParameterSource param = new MapSqlParameterSource()
                .addValue("id", commentId)
                .addValue("creatorId", webContext.getUserId())
                .addValue("role", webContext.getRole() == Roles.STUDENT ? 1 : 0);

        return namedParameterJdbcTemplate.queryForObject(sql.toString(), param, Integer.class);
    }


}
